package com.koron.css2.meterService.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterBoreBean;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.meterService.bean.query.MsChgPlanQueryBean;
import com.koron.css2.meterService.dto.MsChgNewDto;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsChgPlanMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 简易版换表列表查询
 * @author admit
 * @version 2.1
 * @since 2021.10.15T *
 */
public class MsChgQueryNew implements ServerInterface{
	
	private static Logger log = LogManager.getLogger(MsChgQueryNew.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		MessageBean<MsChgNewDto> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", MsChgNewDto.class);
		try {
			MsChgPlanQueryBean query = JsonUtils.objectToPojo(req.getData(), MsChgPlanQueryBean.class);
			MsChgPlanMapper mapper = factory.getMapper(MsChgPlanMapper.class);
			List<MsChgNewDto> data = mapper.selectNewMsChgPlanList(query);
			if(data.size() == 0) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "未找到换表记录", Void.class);
			}
			MsChgNewDto msChgNew = data.get(0);
			MeterModelMapper meterModelMapper = factory.getMapper(MeterModelMapper.class);
			MeterModelBean oldMeterModel = meterModelMapper.selectMeterModelById(msChgNew.getOldMeterModel());
			if(oldMeterModel != null) {
				msChgNew.setOldMeterModelName(oldMeterModel.getModelName());
			}
			MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
			MeterBoreBean oldMeterBore =  meterBoreMapper.selectMeterBoreById(msChgNew.getOldMeterBore());
			if(oldMeterBore != null) {
				msChgNew.setOldMeterBoreName(oldMeterBore.getBoreName());
			}
			MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
			MsInfoDto msInfo = msInfoMapper.getMsInfoByNo(msChgNew.getNewMeterNo());
			msChgNew.setNewMeterBore(msInfo.getMeterBore());
			msChgNew.setNewMeterModel(msInfo.getMeterModel());
			msChgNew.setNewMeterFactory(msInfo.getMeterFactory());
			msChgNew.setNewMeterType(msInfo.getMeterType());
			Map<String, String> meterTypes = Tools.mapDicByCode(factory, "MMT");
			if(StringUtils.isNotBlank(msChgNew.getNewMeterBore())) {
				MeterBoreBean boreBean =  meterBoreMapper.selectMeterBoreById(msChgNew.getNewMeterBore());
				msChgNew.setNewMeterBoreName(boreBean.getBoreName());
			}
			if (StringUtils.isNotBlank(msChgNew.getNewMeterType())) {
				msChgNew.setNewMeterTypeName(meterTypes.get(msChgNew.getNewMeterType()));
			}
			/*
			ConfigMapper configMapper = factory.getMapper(ConfigMapper.class);
			String meterModelStruct = configMapper.getConfigValueByName("MR_WATER_MODEL_STRUCT");
			if(Objects.equal("1", meterModelStruct) && StringUtils.isNotBlank(msChgNew.getNewMeterModel())) {
				MeterModelBean meterModel = meterModelMapper.selectMeterModelById(msChgNew.getNewMeterModel());
				msChgNew.setNewMeterModelName(meterModel.getModelName());
			}else {
				msChgNew.setNewMeterModelName(msChgNew.getNewMeterModel());
			}*/
			MeterModelBean meterModel = meterModelMapper.selectMeterModelById(msChgNew.getNewMeterModel());
			if(meterModel != null){
				msChgNew.setNewMeterModelName(meterModel.getModelName());
			}else{
				msChgNew.setNewMeterModelName(msChgNew.getNewMeterModel());
			}
			if(StringUtils.isNotBlank(msChgNew.getNewMeterFactory())) {
				 MeterFactoryMapper meterFactoryMapper = factory.getMapper(MeterFactoryMapper.class);
			     MeterFactoryBean meterFactory = meterFactoryMapper.selectMeterFactoryById(msChgNew.getNewMeterFactory());     
			     msChgNew.setNewMeterFactoryName(meterFactory.getFactoryName());
			}
			msChgNew.setImei(msInfo.getImei());
			msChgNew.setImsi(msInfo.getImsi());
			msChgNew.setMaxValue(msInfo.getMaxValue() == null ? null : msInfo.getMaxValue().longValue());
			Map<String, String> handleWays = Tools.mapDicByCode(factory, "OMD"); //换表类型
			msChgNew.setOldHandleWayName(handleWays.get(msChgNew.getOldHandleWay() + ""));
			info.setData(msChgNew);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription("换表查询失败");
			log.error(e.getMessage(), e);
		}
		return info;
	}
}
